﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using JLL.China.Web.Libraries;
using System.Drawing;
using JLL.China.Infrastructure.Services;
using JLL.China.Model;
using System.Data.Linq;
namespace JLL.China.Web
{

    public class UploadMedia : IHttpHandler
    {
     
        
        public void ProcessRequest(HttpContext context)
        {
            
            string imgType = context.Request.Form["imageType"].ToString();
           
            HttpPostedFile file = context.Request.Files["FileData"];
            string mediaRootFolder = "MediaFiles";
            string mediaRootPath = context.Server.MapPath("~/Media/" + mediaRootFolder + "/");
            string storagePath = mediaRootPath ;
            if (!Directory.Exists(storagePath))
            {
                Directory.CreateDirectory(storagePath);
                //Directory.Delete(storagePath);
            }
            // clearFolder(storagePath);

            string ext = System.IO.Path.GetExtension(file.FileName);
            string filenameguid = Guid.NewGuid().ToString();
            string fullFilePath = storagePath + "\\" +filenameguid + ext;


            file.SaveAs(fullFilePath);
           
            context.Response.Write("Success");


        }

        private void clearFolder(string FolderName)
        {
            DirectoryInfo dir = new DirectoryInfo(FolderName);

            foreach (FileInfo fi in dir.GetFiles())
            {
                fi.Delete();
            }

            foreach (DirectoryInfo di in dir.GetDirectories())
            {
                clearFolder(di.FullName);
                di.Delete();
            }
        }
        //
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
        private byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }
        public Image byteArrayToImage(byte[] byteArray)
        {
            MemoryStream ms = new MemoryStream();

            ms = new MemoryStream(byteArray, 0, byteArray.Length);
            ms.Seek(0, SeekOrigin.Begin);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            return returnImage;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
